# This build script automates the entire release process of releasing MoqContrib
# 
# To run this, install ruby, rake and albacore
#
# >>> rake
#

require 'albacore'
$ilmerge = 'C:\Program Files (x86)\Microsoft\ILMerge\ilmerge.exe'

desc "clean this project"
task :clean do
	rm_rf $build_dir
end

desc "Build and merge assemblies"
task :default => [:merge]

$assys = [
	"MoqContrib.AutoMock.Castle",
	"MoqContrib.AutoMock.Autofac",
	"MoqContrib.AutoMock.CommonServiceLocator",
]

$test_assys = [
	#"MoqContrib.AutoMock.Autofac.Tests",
	"MoqContrib.AutoMock.Castle.Tests",
	"MoqContrib.AutoMock.CommonService.Tests",
	"MoqContrib.AutoMock.All.Tests",
	"MoqContrib.AutoMock.Tests"
]

desc "build the whole solution as release"
msbuild :build do |msb|	
	msb.properties :configuration => :Release
	msb.targets :Clean, :Build
	msb.solution = "Source/MoqContrib.sln"
end

desc "run all unit tests for release configuration"
nunit :test => :build do |nunit|
	assys = []
	$test_assys.each { |f|
		assys << "Source/#{f}/bin/Release/#{f}.dll"
	}
	nunit.assemblies = assys
	nunit.command = "packages/NUnit.2.5.9.10348/Tools/nunit-console.exe" 
	nunit.options '/framework v4.0'
end

$build_dir = 'releasable'
file $build_dir do
	mkdir $build_dir
end

task :merge => 'merge:all'

namespace :merge do

	$target_assys = []
	$assys.each do |a|
		assy = "#{$build_dir}\\#{a}.dll"
		file assy => [$build_dir, :build] do |f|
			cd = Dir.pwd.gsub("/", "\\")
			puts `#{$ilmerge} /out:#{cd}\\#{assy} #{cd}\\Source\\#{a}\\bin\\Release\\#{a}.dll #{cd}\\Source\\#{a}\\bin\\Release\\MoqContrib.AutoMock.dll`
		end
		$target_assys << assy
	end

	task :all => $target_assys do
		puts 'finished merge'
	end

	task :test_deps do 
		cp "packages/NUnit.2.5.9.10348/lib/nunit.framework.dll", $build_dir
		cp "packages/Moq.4.0.10827/lib/NET35/Moq.dll", $build_dir
		cp "packages/Castle.Windsor.2.5.2/lib/NET35/Castle.Windsor.dll", $build_dir
		cp "packages/Castle.Core.2.5.2/lib/NET35/Castle.Core.dll", $build_dir
		cp "packages/Autofac.2.4.5.724/lib/NET35/Autofac.Configuration.dll", $build_dir
		cp "packages/Autofac.2.4.5.724/lib/NET35/Autofac.dll", $build_dir
		cp "packages/CommonServiceLocator.1.0/lib/NET35/Microsoft.Practices.ServiceLocation.dll", $build_dir
	end

	# I'm considering dropping this part, it doesn't really work
	$integration_tests = []
	$assys.each do |a|
		assy = "#{$build_dir}\\#{a}.Tests.dll"
		file assy => [$build_dir, :all, :test_deps] do 
			cp "Source/#{a}.Tests/bin/Release/#{a}.Tests.dll", $build_dir
		end
		$integration_tests << assy
	end

	nunit :test_merged => $integration_tests do |nunit|
		nunit.assemblies = $integration_tests
		nunit.command = "packages/NUnit.2.5.9.10348/Tools/nunit-console.exe" 
		nunit.options '/framework v4.0'
	end

end

desc "build all nuget packages"
task :package => 'package:create'

namespace :package do

	def create_spec(name, dep_version)
		nuspec "spec-#{name}" => "releasable/#{name}" do |nu|
			puts "made #{name}"
			nu.id = "MoqContrib.AutoMock.#{name}"
			nu.version = "1.0.0"
			nu.authors = "Tim Kellogg, Kaleb Pedersen"
			nu.description = "An automocking container for Moq implementing the #{name} IoC container"
			nu.title = "MoqContrib #{name} AutoMocking Container"
			nu.language = "en-US"
			nu.projectUrl = 'http://moqcontrib.codeplex.com'
			nu.licenseUrl = 'http://www.opensource.org/licenses/ms-pl'
			nu.dependency :Moq, '4.0'
			nu.dependency name, dep_version
			nu.working_directory = "releasable/#{name}"
			nu.output_file = "#{name}.nuspec"
		end
	end
	
	@packages = {:Castle => '2.5', :CommonServiceLocator => '1.0', :Autofac => '2.4'}
	
	task :define_all => 'rake:merge' do
		@packages.keys.each{ |key|
			directory "releasable/#{key}"
			create_spec key, @packages[key]
			Rake::Task["spec-#{key}"].invoke 
			mv "releasable/MoqContrib.AutoMock.#{key}.dll", "releasable/#{key}"
			mv "releasable/MoqContrib.AutoMock.#{key}.pdb", "releasable/#{key}"
		}
	end
	
	desc "Create nuspec files for all packages"
	task :define => :define_all
	
	directory :output
	
	task :create => :define do
		@packages.keys.each do |name|
			nugetpack "pack-#{name}" => :output do |nu|
				nu.nuspec = "releasable/#{name}/#{name}.nuspec"
				nu.base_folder = "releasable/#{name}"
				nu.output = "output"
			end
			Rake::Task["pack-#{name}"].invoke
		end
	end
	
end






